﻿using System;
using System.Drawing;
using System.Globalization;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace MobileCarManager.MobileControls.Styles
{
  /// <summary>
  /// Implements the manager class for the style handling. This class can be used to
  /// load an save application styles. Styles are saved in an XML file. This class is
  /// implemented as a singleton.
  /// </summary>
  public class StyleManager
  {
    /// <summary>Singleton instance of this class.</summary>
    private static StyleManager _instance;
    /// <summary>Serializer object for the style data structure.</summary>
    private XmlSerializer styleSerializer = new XmlSerializer(typeof(Styles));
    /// <summary>Instance of the root element of the styles.</summary>
    private Styles _styles;

    /// <summary>Constructor.</summary>
    private StyleManager()
    {
      LoadStyle("DefaultStyles.xml");
      if (null == _styles)
        _styles = new Styles();
    }

    /// <summary>Returns the singleton instance of the StyleManager.</summary>
    public static StyleManager Instance
    {
      get
      {
        if (null == _instance)
          _instance = new StyleManager();

        return _instance;
      }
    }

    /// <summary>Returns the root element of the style data structure.</summary>
    public Styles Styles
    {
      get { return _styles; }
    }

    /// <summary>Loads the styles from an XML file.</summary>
    public bool LoadStyle(string fileName)
    {
      try
      {
        XmlTextReader reader = new XmlTextReader(fileName);
        _styles = (Styles)styleSerializer.Deserialize(reader);
      }
      catch (Exception ex)
      {
        // TODO: Trace the exception...
        return false;
      }
      return true;
    }

    /// <summary>Saves the styles to an XML file.</summary>
    public bool SaveStyles(string fileName)
    {
      try
      {
        XmlTextWriter writer = new XmlTextWriter(fileName, Encoding.UTF8);
        styleSerializer.Serialize(writer, _styles);
      }
      catch (Exception ex)
      {
        // TODO: Trace the exception...
        return false;
      }
      return true;
    }

    /// <summary>Helper method to serialize a color.</summary>
    /// <returns>Color definition as a string.</returns>
    /// <param name="color">Color that should be serialized.</param>
    internal static string SerializeColor(Color color)
    {
      return string.Format(CultureInfo.InvariantCulture, "{0}:{1}:{2}", color.R, color.G, color.B);
    }

    /// <summary>Helper method to deserialize a color.</summary>
    /// <returns>The color.</returns>
    /// <param name="color">Color definition as a string.</param>
    internal static Color DeserializeColor(string color)
    {
      byte r, g, b;
      string[] pieces = color.Split(new char[] { ':' });
      r = byte.Parse(pieces[0]);
      g = byte.Parse(pieces[1]);
      b = byte.Parse(pieces[2]);
      return Color.FromArgb(r, g, b);
    }

    /// <summary>Helper method to serialize a font.</summary>
    internal static string SerializeFont(Font font)
    {
      return string.Format(CultureInfo.InvariantCulture, "{0}:{1}:{2}", font.Name, font.Size, font.Style);
    }

    /// <summary>Helper method to deserialize a font.</summary>
    internal static Font DeserializeFont(string font)
    {
      string[] pieces = font.Split(new char[] { ':' });
      string name = pieces[0];
      float size = float.Parse(pieces[1]);
      FontStyle style = (FontStyle)Enum.Parse(typeof(FontStyle), pieces[2], true);
      return new Font(name, size, style);
    }
  }
}
